# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import traceback
import json
from django.http import JsonResponse
from utils.logger import Md_Debug
from app.manage.financeManage.common.manage_query import FinanceManageDB
from app.manage.financeManage.common.manage_add_edit_delete import FinanceEditDB
from django.views.decorators.csrf import csrf_exempt
from utils.response_utils import response_change

FinanceManageDB = FinanceManageDB()

FinanceEditDB = FinanceEditDB()


@csrf_exempt
def finance_list(request):
    """财务管理（任务列表）"""
    try:
        body = json.loads(request.body)
        data = FinanceManageDB.finance_list(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def add_finance(request):
    """新增任务"""
    try:
        body = json.loads(request.body)
        data = FinanceEditDB.add_finance(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def start_finance(request):
    """开启任务、结束任务"""
    try:
        body = json.loads(request.body)
        data = FinanceEditDB.edit_task_status(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def finance_info(request):
    """查看任务详情信息"""
    try:
        body = json.loads(request.body)
        data = FinanceManageDB.finance_info(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def add_finance_report(request):
    """写日报"""
    try:
        body = json.loads(request.body)
        data = FinanceEditDB.add_finance_report(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def edit_task(request):
    """编辑任务"""
    try:
        body = json.loads(request.body)
        data = FinanceEditDB.edit_task(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)
